package com.fingard.dsp.bank.directbank.nnbill02;

import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;
import com.fingard.text.StringHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class NNBill02CloseOrder extends NNBill02Base implements ICloseOrder {

    @Override
    public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req) {
        RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "order");
            if(actItem == null){
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号:[" + p_req.ownAct + "]相关商户配置信息!";
            }
            Map<String, Object> reqmap = new HashMap<String, Object>();
            reqmap.put("memberCode",actItem.inMerchantId);
            reqmap.put("requestTime", Format.DateTime14Format.format(new Date()));
            reqmap.put("origExternalTraceNo",p_req.rdSeq);
            reqmap.put("externalTraceNo",System.currentTimeMillis()+"");
            reqmap.put("merchantName",actItem.userCode);
            reqmap.put("merchantId",actItem.merchantId);
            reqmap.put("terminalId",actItem.termId);
            reqmap.put("amt",p_req.amount);
            reqmap.put("termTxnTime",Format.DateTimeFormat.format(new Date()));
            String digest = sign(reqmap,actItem.saltKey);
            WriteBankLogLn("生成签名串："+digest);
            reqmap.put("secretInfo",digest);
            WriteBankUrlLogLn(bankFront.serverURL+"/cancel");
            String[] tmpStrRet = sendToBank(reqmap,bankFront.getUrl("")+"/cancel");
            if(tmpStrRet[0].length() == 0){
                JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                retAts.respCode = "0";
                retAts.respInfo = resultObj.get("responseMessage").toString();

                String responseCode = resultObj.get("responseCode").toString();
                if("00".equals(responseCode)){
                    if(resultObj.get("txnFlg").toString().equals("V")){
                        retAts.closeState = CloseOrderState.Close_Success;
                    }else{
                        retAts.closeState = CloseOrderState.Close_Fail;
                        retAts.errorCode = responseCode + "+" + resultObj.get("txnFlg").toString();
                        retAts.errorMsg = resultObj.get("responseMessage").toString();
                    }
                }else{
                    retAts.respCode = "-1";
                    retAts.closeState = CloseOrderState.Close_Fail;
                    retAts.errorCode = responseCode + "+" + resultObj.get("txnFlg").toString();
                    retAts.errorMsg = resultObj.get("responseMessage").toString();
                }
            }else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[0];
            }
        }catch (Exception ex) {
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            ex.printStackTrace();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
